package org.mtmi.ui.controls.midi;

import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.ui.IMG;
import org.mtmi.ui.controls.IValueGenerator;
import org.mtmi.ui.controls.listeners.ControlBoundsEvent;
import org.mtmi.ui.controls.listeners.ControlBoundsListener;
import org.mtmi.ui.edit.AbstractSourceControlConnector;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.links.CID;
import org.mtmi.ui.links.IConnectable;

/**
 * The idea is to connect a Keyboard to a Slider or Knob which control keyboard transporte.
 * 
 */
public class MidiControllerValueConnector extends AbstractSourceControlConnector
  implements ControlBoundsListener
{
	
	private MidiController controller;
	
	public MidiControllerValueConnector( MidiController k, int _flags) {
		super( k.getScene(), IMG.GetD( IMG.I_MIDI_VALUE ), CID.VALUE_CONSUMER, _flags );
		controller = k;
		setConnectedControl( (IConnectable)controller.getValueController(),
				              controller.getValueControllerConnectionId() );
		
		setSize( computeSize( new LayoutData(scene) ) );
		updateLocation();
		
		controller.addControlBoundsListener( this );
	}
	
	@Override
	public void dispose() {
		controller.removeControlBoundsListener( this );
		super.dispose();
	}

	@Override
	protected void newConnection(IConnectable c, String cid, int x, int y, String connection_id) {
		controller.setValueController( (IValueGenerator)c, cid );
	}

	@Override
	public void controlBoundsChanged(ControlBoundsEvent event) {
		updateLocation();
	}
	
	private void updateLocation() {
		Rectangle r = controller.getBounds(null);
		setLocation( r.x+10, r.y+r.height-size.y/2 );
	}
	
}
